import React 			from 'react';
import { Link }			from 'react-router-dom'
import PageTitle    	from 'component/pagetitle/index.jsx';
import Pagination 		from 'util/pagination/index.jsx';
import MUtil        	from 'util/mm.jsx'
import Order         	from 'service/order-service.jsx';
import TableView		from 'component/tableview/index.jsx';
import ListSearch		from 'util/index-list-search/index.jsx';


const _util 			= new MUtil();
const _order 			= new Order();
class OrderList extends React.Component{
	
	constructor(props){
		super(props);
		this.state = {
			 pageNum  : 1,
			 list     : [],
			 listType : 'list'  //list /search
		}
	}
	
	componentDidMount(){
		this.loadOrderList();
	}
	
	loadOrderList(){
		let listParam = {};
		listParam.pageNum = this.state.pageNum;
		listParam.listType = this.state.listType;
		if (this.state.listType === 'search') {
			listParam.orderNo = this.state.orderNo;
		}
		
		_order.getOrderList(listParam)
			.then(res => {
				console.log(res);
				this.setState(res);
			},err =>{
				this.setState({list:[]});
				_util.errorTips(err);
			})
	}
	

	onChangePagination(page){
		this.setState({
			pageNum: page,
		},() => {
			this.loadOrderList();
		});
	}
	
	onSearch(type,keyWord){
		this.setState({
			orderNo		:	keyWord,
			listType	:	'search'
		},() => {
			this.loadOrderList();
		});
	}
	
	render(){
		const optionalType = [{name:'按订单号查询',value:'orderNo'}];
		const tbodyList = this.state.list.map((order,index) => {
			return (
				<tr key={index}>
					<td>
						<Link to={`/order/detail/${order.orderNo}`}>{order.orderNo}</Link>
					</td>
					<td>{order.receiverName}</td>
					<td>{order.statusDesc}</td>
					<td>{order.payment}</td>
					<td>{order.createTime}</td>
					<td className="text-center">
						<Link to={`/order/detail/${order.orderNo}`}>查看</Link>
					</td>
				</tr>
			)
		});
		
		return (
			<div id="page-wrapper">
				<PageTitle title='订单管理' />
				<ListSearch search={(searchType,searchKeyWord) => {this.onSearch(searchType,searchKeyWord)}}
							optionalType={optionalType}/>
				<TableView theaders={['订单号','收件人','订单状态','订单总价','创建时间','操作']} >
					{tbodyList}
				</TableView>
				
				{this.state.list.length > 0 
					? <Pagination 
						onChange={page => {this.onChangePagination(page)}}  
						current={this.state.pageNum}  
						total={this.state.total} /> 
						
					: ''
				}
			</div>
		)
	}
}

export default OrderList;